package com.example.plist.controller;

import com.example.plist.db.pojo.GraphProject;
import com.example.plist.db.pojo.GraphProjectContext;
import com.example.plist.db.pojo.GraphProjectImage;
import com.example.plist.db.pojo.ProjectContext;
import com.example.plist.dto.ExportDTO;
import com.example.plist.dto.ProjectContextDTO;
import com.example.plist.service.GraphProjectService;
import com.example.plist.util.Result;
import com.example.plist.vo.GraphProjectVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GraphController {
  @Resource
  private GraphProjectService graphProjectService;

  @PostMapping("/graph/project/create")
  @ResponseBody  Result createProject(){
      Map<String,Object> result=new HashMap<>();


      return  Result.ok(result);
  }
    @PostMapping("/graph/project/list")
    @ResponseBody  Result getProjectList(){
        Map<String,Object> result=new HashMap<>();

        List<GraphProject> list = graphProjectService.getList(new GraphProject());
        result.put("data",list);
        return  Result.ok(result);
    }


  @GetMapping("/graph/project/export/zip/{id}")
  void exportZip(@PathVariable Integer id, HttpServletResponse response){
      ExportDTO exportDTO = new ExportDTO();
      exportDTO.setId(id);
          graphProjectService.exportZip(exportDTO,response);

  }
  @PostMapping("/graph/project/upload")
  @ResponseBody  Result upLoadFile(String fileName, String key, MultipartFile file){
//      file.getInputStream();
      String src = graphProjectService.uploadFile(fileName,  key,  file);
      Map<String,Object> result=new HashMap<>();

      result.put("data",src);
      return  Result.ok(result);
  }

    @GetMapping("/graph/project/{id}")
    @ResponseBody Result  getProject(@PathVariable(name = "id") Integer id){

     GraphProjectVO projectVO= graphProjectService.findByProjectId(id);
      Map<String,Object> result=new HashMap<>();

      result.put("data",projectVO);
      return  Result.ok(result);
  }
  @DeleteMapping("/graph/project/context/{id}")
  @ResponseBody Result  deleteProject(@PathVariable(name = "id") Integer id){

      Integer r= graphProjectService.deleteContext(id);
      Map<String,Object> result=new HashMap<>();

      result.put("data",r);
      return  Result.ok(result);
  }



    @PostMapping("/graph/project/save/{type}")
    @ResponseBody Result  saveProjectImage(@PathVariable(name = "type") String type,@RequestBody ProjectContextDTO data){

        Map<String,Object> result=new HashMap<>();

        Integer id=null;
        if("image".equals(type)){

            id = graphProjectService.saveImage(data);
        }else{

            id = graphProjectService.saveProjectContext(data);
        }



        result.put("data",id);
        return  Result.ok(result);
    }
}
